package com.atguigu.gulimall.seckill.config;

import com.atguigu.gulimall.seckill.servcie.SeckillSkuService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class SeckillSkuScheduled {

    @Autowired
    SeckillSkuService seckillSkuService;
    @Autowired
    RedissonClient redissonClient;
    private final String upload_lock = "seckill:upload:lock";
    /**
     * 每天凌晨3点上架最近3天需要上架的商品
     */
    @Scheduled(cron = "0 * * * * ?")
    public void uploadSeckillSkuLatest3Days(){
        // 重复上架无需处理
        System.out.println("上架秒杀商品");
        // 加入分布式锁
        RLock lock = redissonClient.getLock(upload_lock);
        lock.lock(10, TimeUnit.SECONDS);
        try{
            seckillSkuService.uploadSeckillSkuLatest3Days();
        }finally {
            lock.unlock();
        }



    }
}
